home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5060 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.1 KB  |  91 lines

  1. Path: soap.news.pipex.net!pipex!usenet
  2. From: Barry Warburton <ge68@dial.pipex.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ Gurus! Is it correct?
  5. Date: 2 Feb 1996 11:12:48 GMT
  6. Organization: UnipalmPIPEX server (post doesn't reflect views of UnipalmPIPEX)
  7. Message-ID: <4esrjg$g6l@soap.news.pipex.net>
  8. References: <4eqvtg$cg5@israel-info.datasrv.co.il>
  9. NNTP-Posting-Host: aj105.du.pipex.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1 (Windows; I; 16bit)
  14.  
  15. dmitry@enigma.co.il (Dmitry A. Davidovich) wrote:
  16. >Is next code is correct from point of view of pure C++ ?
  17. >
  18. >class A
  19. >{
  20. >};
  21. >
  22. >class B
  23. >{
  24. >};
  25. >
  26. >class C : public A, public B
  27. >{
  28. >};
  29. >
  30.  
  31. int main(){
  32. >A* pA = new C;
  33. // pA= (A*) new C;
  34. >B* pB = new C;
  35. // pB= (B*) new C;
  36. >
  37. >delete pA;
  38. >delete pB;
  39. >
  40. return 0;
  41. }
  42.  
  43. >Please, mail me copy of response.
  44. >
  45. >+++++++++++++++++++++++++++++++++++++++++
  46. >Dmitry Davidovich
  47. >CS Tel Aviv University
  48. >dmitry@enigma.co.il
  49. >ddmitry@libra.math.tau.ac.il
  50. >+++++++++++++++++++++++++++++++++++++++++
  51. >
  52. The thing that you wanted to ask is whether we can delete
  53. the references to the multiple inherited class without
  54. corrupting memory.
  55.  
  56. ANSWER
  57. This depends on the compiler, 
  58. But as usual with C++ newer compiler versions give a better chance of 
  59. success.
  60.  
  61. The multiple inheritance problem is usually solved by making a pointer
  62. conversion which changes the address of the pointer.
  63.  
  64. A pA= new C; // pA points to the A part of C
  65. B pB= new C; // pB points to the B part of C
  66.  
  67. delete pA; // delete A address
  68. delete pB; // delete B address
  69.  
  70. In some C++ compilers you are prohibited to do these conversions,
  71. unless you specify one of the base-classes as virtual, which will
  72. allow you to do only one conversion.
  73.  
  74. A compiler that would compile it correctly would keep a seperate 
  75. delete offset in the virtual-function table of the class.
  76.  
  77. delete pA becomes:
  78. free( pA+ pA->class->deleteOffset);
  79. free( pB+ pB->class->deleteOffset);
  80.  
  81. The implementation will differ accross compilers,
  82. if the compiler DID implement it.
  83.  
  84. I hope this answers your questions.
  85.  
  86.  
  87. Dirk Wessels
  88. Deleloper C++/Delphi/Smalltalk
  89.  
  90.  
  91.